package cn.lingyangwl.agile.pay.pay.channel.wxpay;

import cn.lingyangwl.agile.pay.pay.channel.wxpay.utils.WxpayKit;
import cn.lingyangwl.agile.pay.pay.channel.wxpay.utils.WxpayV3Util;
import cn.lingyangwl.agile.pay.common.constants.Cs;
import cn.lingyangwl.agile.pay.constant.IfCodeEnum;
import cn.lingyangwl.agile.pay.model.entity.PayOrder;
import cn.lingyangwl.agile.pay.pay.channel.AbstractPayOrderCloseService;
import cn.lingyangwl.agile.pay.pay.model.bo.MchAppConfigContext;
import cn.lingyangwl.agile.pay.pay.model.bo.WxServiceWrapper;
import cn.lingyangwl.agile.pay.pay.model.params.wxpay.WxpayIsvsubMchParams;
import cn.lingyangwl.agile.pay.pay.model.rqrs.ChannelRetMsgExt;
import cn.lingyangwl.agile.pay.pay.service.ConfigContextQueryService;
import com.alibaba.fastjson2.JSONObject;
import com.github.binarywang.wxpay.bean.request.WxPayOrderCloseRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 微信关闭订单
 *
 * @author shenguangyang
 */
@Service
public class WxpayPayOrderCloseService extends AbstractPayOrderCloseService {

    @Resource
    private ConfigContextQueryService configContextQueryService;

    @Override
    public IfCodeEnum getIfCode() {
        return IfCodeEnum.WXPAY;
    }

    @Override
    public ChannelRetMsgExt close(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) {
        try {
            WxServiceWrapper wxServiceWrapper = (WxServiceWrapper) configContextQueryService
                    .getClientWrapper(this.getIfCode(), mchAppConfigContext);

            if (Cs.PAY_IF_VERSION.WX_V2.equals(wxServiceWrapper.getApiVersion())) {  //V2
                WxPayOrderCloseRequest req = new WxPayOrderCloseRequest();

                // 放置isv信息
                WxpayKit.putApiIsvInfo(mchAppConfigContext, req);

                req.setOutTradeNo(payOrder.getPayOrderNo());

                WxPayService wxPayService = wxServiceWrapper.getWxPayService();

                WxPayOrderCloseResult result = wxPayService.closeOrder(req);

                if ("SUCCESS".equals(result.getResultCode())) { //关闭订单成功
                    return ChannelRetMsgExt.confirmSuccess(null);
                } else if ("FAIL".equals(result.getResultCode())) { //关闭订单失败
                    return ChannelRetMsgExt.confirmFail(); //关闭失败
                } else {
                    return ChannelRetMsgExt.waiting(); //关闭中
                }

            } else if (Cs.PAY_IF_VERSION.WX_V3.equals(wxServiceWrapper.getApiVersion())) {   //V3
                String reqUrl;
                JSONObject reqJson = new JSONObject();
                if (mchAppConfigContext.isIsvsubMch()) { // 特约商户
                    WxpayIsvsubMchParams isvsubMchParams = (WxpayIsvsubMchParams) configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), getIfCode());
                    reqUrl = String.format("/v3/pay/partner/transactions/out-trade-no/%s/close", payOrder.getPayOrderNo());
                    reqJson.put("sp_mchid", wxServiceWrapper.getWxPayService().getConfig().getMchId());
                    reqJson.put("sub_mchid", isvsubMchParams.getSubMchId());
                } else {
                    reqUrl = String.format("/v3/pay/transactions/out-trade-no/%s/close", payOrder.getPayOrderNo());
                    reqJson.put("mchid", wxServiceWrapper.getWxPayService().getConfig().getMchId());
                }

                WxpayV3Util.closeOrderV3(reqUrl, reqJson, wxServiceWrapper.getWxPayService());
                return ChannelRetMsgExt.confirmSuccess(null);
            }
            return ChannelRetMsgExt.confirmFail();
        } catch (WxPayException e) {
            return ChannelRetMsgExt.sysError(e.getReturnMsg());
        } catch (Exception e) {
            return ChannelRetMsgExt.sysError(e.getMessage());
        }
    }

}
